import { ExtractJwt, Strategy } from 'passport-jwt';
import { PassportStrategy } from '@nestjs/passport';
import { HttpException, Injectable } from '@nestjs/common';
import config from 'src/config';
import * as jwt from 'jsonwebtoken';
import { AuthService } from './app.service';

@Injectable()
export class AppStrategy extends PassportStrategy(Strategy,'app-jwt') {
	constructor(
		private readonly authService:AuthService
	) {
		super({
			jwtFromRequest: ExtractJwt.fromAuthHeaderAsBearerToken(),
			ignoreExpiration: false,
			secretOrKey: config.app.secret,
		});
	}

	async validate(payload: any) {
		return payload
	}
}


export function validateJwt(authorization:string){
	try {
		const token = authorization.split(' ')[1];
		const jwtPayload:any=jwt.verify(token, config.app.secret);
		return jwtPayload
	} catch (error) {
		return null;
	}
	return null;
}
